package com.flycms.module.feedback.service;

import com.flycms.core.entity.PageVo;
import com.flycms.module.feedback.dao.FeedbackDao;
import com.flycms.module.feedback.model.Feedback;
import com.flycms.module.feedback.param.FeedbackParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FeedbackService {

    @Autowired
    private FeedbackDao feedbackDao;

    /**
     * 保存意见
     * @param feedback 意见信息
     */
    public void save(Feedback feedback) {
        feedback.setCreatedTime(new Date());
        feedback.setUpdatedTime(new Date());
        feedbackDao.insertSelective(feedback);
    }

    /**
     * 分页查询意见反馈
     * @param param
     * @return 结果集
     */
    public PageVo<Feedback> getPage(FeedbackParam param) {
        List<Feedback> feedbackList =
                feedbackDao.getPage(param, (param.getPageIndex()-1)*param.getPageSize(),param.getPageSize());
        if(CollectionUtils.isEmpty(feedbackList)){
            return new PageVo<>(param.getPageIndex());
        }
        return new PageVo<>(param.getPageIndex(), feedbackList.size(), feedbackList);
    }

    public void delById(Long id) {
        feedbackDao.delById(id);
    }
}
